package com.my.vote.widget;

import android.content.Context;
import android.support.v7.widget.AppCompatTextView;
import android.text.Html;
import android.util.AttributeSet;
import android.widget.TextView;

import org.reactivestreams.Subscriber;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;


/**倒计时控件
 * @date 2016/10/25
 */
public class RxTimeButton extends TextView implements Consumer<String> {

    private Disposable subscription;

    public RxTimeButton(Context context) {

        super(context);
    }

    public RxTimeButton(Context context, AttributeSet attrs) {

        super(context, attrs);
    }

    public RxTimeButton(Context context, AttributeSet attrs, int defStyleAttr) {

        super(context, attrs, defStyleAttr);
    }

    public void stopInterval() {
        if (subscription != null) {
            subscription.dispose();
        }
    }

    final String D = "%dDay%02d:%02d:%02d";

    final String H = "%02d:%02d:%02d";

    /**
     * @param second
     */
    public void startInterval(final long second, Action onComplete) {

        if (second <= 0) {
            return;
        }

        this.setText("");
        setEnabled(false);
        if (subscription != null) {
            subscription.dispose();
        }
        subscription = Observable.interval(0, 1, TimeUnit.SECONDS).subscribeOn(Schedulers.io()).unsubscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).map(new Function<Long, String>() {

            @Override
            public String apply(Long increaseTime) throws Exception {
                long time = second - increaseTime.intValue();

                return getStringTime(time);

            }

        }).take(second + 1).subscribe(this, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {

            }
        },onComplete);
    }

    private String getStringTime(long second) {
        long day = 0L;
        long hour = 0L;
        long minute = 0L;
        if (second >= 60) {
            //取整
            minute = second / 60;
            //取余
            second = second % 60;
        }

        if (minute >= 60) {
            hour = minute / 60;
            minute = minute % 60;
        }

        if (hour >= 24) {
            day = hour / 24;
            hour = hour % 24;
        }

        return day > 0 ? String.format(D, day, hour, minute, second) : String.format(H, hour, minute, second);
    }


    @Override
    public void accept(String integer) throws Exception {
        this.setText(integer);
    }

    @Override
    protected void onDetachedFromWindow() {

        if (subscription != null) {
            subscription.dispose();
        }
        super.onDetachedFromWindow();

    }
}